﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2021/5/14 14:28:21 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;
using System.IO;




namespace MTQ.Web.Controllers
{

    public class Log_WebController : BaseController
    {

        private ILog_WebService ilog_webService = new Log_WebService();


        //                                           
        // GET: //         
        [AccountAuth(Roles = "Log_Web_Index")]
        public ActionResult Index()
        {
            return View();
        }


        [AccountAuth(Roles = "Log_Web_Index")]
        public ActionResult Search()
        {


            ServertLayuiTable<Log_Web> servertBootStrapTable = new ServertLayuiTable<Log_Web>();
            GetSearchData(ref servertBootStrapTable, true);


            return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);
        }

        private void GetSearchData(ref ServertLayuiTable<Log_Web> servertBootStrapTable, bool page)
        {
            Log_WebQuery ilog_webQuery = new Log_WebQuery();
            ilog_webQuery.Searchkey = GetRequestQueryString("keyword");

            ilog_webQuery.UserControlProjectNumber = UserControlProjectNumber;
            if (page)
            {
                servertBootStrapTable = ilog_webService.LoadLayuiSearchData(ilog_webQuery, Request);
            }
            else
            {
                IQueryable<Log_Web> ilog_web = ilog_webService.GetIQueryable(ilog_webQuery);
                servertBootStrapTable.data = ilog_web.ToList();
                servertBootStrapTable.count = ilog_web.Count();
            }
        }








        //          
        // GET: /Log_Web/Create          
        [AccountAuth(Roles = "Log_Web_Create")]
        public ActionResult Create()
        {
            Log_Web log_web = new Log_Web();
            return View(log_web);
        }

        //          
        // POST: /Log_Web/Create          
        [HttpPost]
        [AccountAuth(Roles = "Log_Web_Create")]
        public ActionResult Create(Log_Web log_web)
        {
            ModelMessage listMsg = new ModelMessage();
            if (ModelState.IsValid)
            {
                var result = ilog_webService.AddEntity(log_web);
                listMsg = new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败");
            }
            return Json(listMsg);
        }


        [AccountAuth(Roles = "Log_Web_Edit")]
        public ActionResult Edit(int? primarykey)
        {
            Log_Web log_web = ilog_webService.LoadEntities(x => x.Log_Id == primarykey).FirstOrDefault();
            if (log_web == null)
            {
                return HttpNotFound();
            }
            return View(log_web);
        }

        [HttpPost]
        [AccountAuth(Roles = "Log_Web_Edit")]
        public ActionResult Edit(Log_Web formlog_web)
        {
            ModelMessage listMsg = new ModelMessage();
            if (ModelState.IsValid)
            {
                try
                {
                    Log_Web log_web = ilog_webService.LoadEntities(x => x.Log_Id == formlog_web.Log_Id).First();
                    log_web.Log_Id = formlog_web.Log_Id;
                    log_web.Log_UserId = formlog_web.Log_UserId;
                    log_web.Log_UserName = formlog_web.Log_UserName;
                    log_web.Log_Path = formlog_web.Log_Path;
                    log_web.Log_Remark = formlog_web.Log_Remark;
                    log_web.Log_CreateTime = formlog_web.Log_CreateTime;
                    bool result = ilog_webService.UpdateEntity(log_web);
                    listMsg = new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败");
                }
                catch (Exception e)
                {
                    listMsg = new ModelMessage("ChangeResult", "m002", "保存失败");
                }
            }
            return Json(listMsg);

        }

        [HttpPost]
        [AccountAuth(Roles = "Log_Web_Edit")]
        public ActionResult ActivateSwich(string primarykeys, string activate = "N")
        {
            ModelMessage modelMessage = new ModelMessage();

            if (OperatorAvailable(primarykeys, activate))
            {
                modelMessage.Code = "m001";
                modelMessage.Message = "修改成功";
            }
            else
            {
                modelMessage.Code = "m002";
            }
            return Json(modelMessage, JsonRequestBehavior.AllowGet);
        }

        private bool OperatorAvailable(string primarykeys, string available)
        {
            List<int> listLog_WebGUID = primarykeys.Split(',').ToListInt();
            List<Log_Web> listLog_Web = ilog_webService.LoadEntities(x => listLog_WebGUID.Contains(x.Log_Id)).ToList();
            bool updaterow = ilog_webService.DeleteListEntity(listLog_Web);
            if (updaterow)
            {
                return false;
            }
            return true;
        }


        public ActionResult Export()
        {

            var fieldtitle = GetRequestQueryString("fieldtitle");
            var listFieldTitle = JsonSerializerHelper.GetJsonList<FieldTitle>(fieldtitle);

            ServertLayuiTable<Log_Web> servertBootStrapTable = new ServertLayuiTable<Log_Web>();
            GetSearchData(ref servertBootStrapTable, false);
            List<ExcelColumn> columnInfo = new List<ExcelColumn>();

            if (listFieldTitle != null)
            {
                foreach (var item in listFieldTitle)
                {
                    columnInfo.Add(new ExcelColumn(item.field, item.title));
                }
            }

            byte[] btyBytes = null;
            var filename = Server.MapPath("/") + @"DownloadDocument\" + System.Guid.NewGuid().ToString() + ".xlsx";
            NPIOExportExcel.ExportExcelTest<Log_Web>(servertBootStrapTable.data, filename, ref btyBytes, columnInfo, servertBootStrapTable.data.Count);
            return File(filename, "text/comma-separated-values", "导出" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");

        }



    }
}


